package br.com.webservicedelivery.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.webservicedelivery.dao.ServicosDAO;
import br.com.webservicedelivery.domain.Servicos;

@FacesConverter("servicoConverter")
public class ServicoConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent component,
			String valor) {
		try {
			Long id = Long.parseLong(valor);
			
			ServicosDAO servicosDAO = new ServicosDAO();
			Servicos servicos = servicosDAO.buscarPorId(id);
			
			return servicos;

		} catch (RuntimeException ex) {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent component,
			Object objeto) {
		try {
			Servicos servicos = (Servicos) objeto;
			Long id = servicos.getId();
			
			return id.toString();

		} catch (RuntimeException ex) {
			return null;
		}
	}

}
